package com.top.ruizhong.mode.vm

import android.app.Application
import android.widget.EditText
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPStaticUtils
import com.module.lemlin.base.BaseViewModel
import com.module.lemlin.mode.ViewModelSubscribe
import com.module.lemlin.rxhttp.HttpServiceData
import com.module.lemlin.rxhttp.HttpServiceResponse
import com.top.ruizhong.R
import com.top.ruizhong.app.TOKEN
import com.top.ruizhong.app.USER_INFO
import com.top.ruizhong.entity.UserLoginResponse
import com.top.ruizhong.entity.UserSmsResponse
import com.top.ruizhong.mode.api.userLogin
import com.top.ruizhong.mode.api.userSmsSend
import io.reactivex.rxjava3.disposables.Disposable

class AccessViewModel(application: Application) : BaseViewModel(application) {

    val userSmsLiveData = MutableLiveData<HttpServiceResponse<UserSmsResponse>>()
    val userLoginLiveData = MutableLiveData<HttpServiceResponse<UserLoginResponse>>()

    /**
     * 发送短信验证码
     *
     * @param mobile 手机号
     * @param event 事件：login,set_pwd
     * */
    fun userSmsSend(mobile: String, event: String) {
        val listener = object : ViewModelSubscribe<UserSmsResponse>(userSmsLiveData) {
            override fun onSuccess(resp: UserSmsResponse) {
                super.onSuccess(resp.apply { data = mobile })
            }
        }
        val disposable: Disposable =
            userSmsSend(mobile, event, listener)
        dispose.add(disposable)
    }

    /**
     * 短信验证码登录
     *
     * @param mobile 手机号
     * @param smsCode 短信验证码
     * */
    fun userLogin(mobile: String, smsCode: String) {
        val listener = object : ViewModelSubscribe<UserLoginResponse>(userLoginLiveData) {
            override fun onSuccess(resp: UserLoginResponse) {
                super.onSuccess(resp)
                SPStaticUtils.put(TOKEN, resp.data.token)
                SPStaticUtils.put(USER_INFO, GsonUtils.toJson(resp.data))
            }
        }
        val disposable: Disposable =
            userLogin(mobile, smsCode, listener)
        dispose.add(disposable)
    }


    val contentCheckCodeLiveData = MutableLiveData<CharSequence>()
    val contentMobileLoginLiveData = MutableLiveData<CharSequence>()
    val contentMobileBindLiveData = MutableLiveData<CharSequence>()


    fun observableEditText(view: EditText) {
        view.addTextChangedListener {
            when (view.id) {
                R.id.etLoginMobileContent -> contentMobileLoginLiveData.postValue(
                    if (it.isNullOrEmpty()) null else it.toString()
                )
                R.id.etBindMobileContent -> contentMobileBindLiveData.postValue(
                    if (it.isNullOrEmpty()) null else it.toString()
                )
                R.id.etCheckCodeContent -> contentCheckCodeLiveData.postValue(
                    if (it.isNullOrEmpty()) null else it.toString()
                )
            }
        }
    }

    fun clearLiveDataCheckCode() {
        this.contentCheckCodeLiveData.postValue(null)
    }

    fun clearLiveDataMobileLogin() {
        this.contentMobileLoginLiveData.postValue(null)
    }

    fun clearLiveDataMobileBind() {
        this.contentMobileBindLiveData.postValue(null)
    }
}